//
//  MainViewController.swift
//  XinLang
//
//  Created by 张辉 on 16/5/10.
//  Copyright © 2016年 张辉. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

       
        addChildViewControlelrs()
       
        setUpTabbar()
        
         showNewFeature()
        
    }
    
    

  // MARK: - 设置TabBar分割线
 private func setUpTabbar() {
        //设置字体的颜色
        tabBar.tintColor = UIColor.orangeColor()
        //取消分割线
        tabBar.shadowImage = UIImage.cz_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        //
        tabBar.addSubview(composeButton)
    
         //添加点击事件
        composeButton.addTarget(self, action:#selector(MainViewController.clickComposeButton), forControlEvents: .TouchUpInside)
 }
    
    
  
    
    
    //MARK:---视图即将显示设置按钮的frame
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        // 前置按钮
        tabBar.bringSubviewToFront(composeButton)
        // 设置按钮位置
        let w = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
        composeButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        
    }
    
  //MARK:-监听按钮的点击

  @objc private   func clickComposeButton(){
    
        print("...")
    }
    
   //MARK:---私有控件懒加载按钮(分类)
 private lazy var composeButton :UIButton = UIButton(zh_title: nil,  imageName: "tabbar_compose_icon_add",
     backImageName: "tabbar_compose_button")
   //MARK 添加子控制器
    
    /// 添加子控制器
    ///
    /// - parameter vc:        子控制器
    /// - parameter title:     标题
    /// - parameter imageName: 图像名称
    private func addChildViewControlelrs(){
    
        addChildViewControlelr(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewControlelr(MessageViewController(), title: "消息", imageName: "tabbar_discover")
        
        addChildViewController(UIViewController())
        
        addChildViewControlelr(DiscoverViewController()  , title: "发现", imageName: "tabbar_message_center")
        addChildViewControlelr(ProfileViewController(), title: "我", imageName: "tabbar_profile")
    
    
    
    }
    
    
    
    private  func addChildViewControlelr(homeVc :UIViewController, title :NSString, imageName : NSString){
    
    
       homeVc.title = title as String
    
     //方法二：设置字体属性homeVc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
//        homeVc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .Normal)
        
     //图片
    homeVc.tabBarItem.image =  UIImage(named: imageName as String)
    homeVc.tabBarItem.selectedImage = UIImage(named: (imageName as String) + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
    
    let navVc = UINavigationController(rootViewController: homeVc)
    
    
    addChildViewController(navVc)
    
        
    
    }
    

}
// MARK: - 新特性处理
extension MainViewController{

    
    /// 显示新特性
    private func showNewFeature() {
        // 1. 如果没有登录直接返回
        if !UserAccount.sharUserAccount.isLogin {
            return
        }
        
        // 2. 判断是否有新版本
//        let v = isNewVersion ? NewFeatureView() : WelcomeView()
        let v = WelcomeView()
        // 3. 将视图添加到当前视图
        view.addSubview(v)
    }
    
    /// 是否有新版本
    private var isNewVersion: Bool {
        // 1. 获取当前版本 1.2
        let currentVersion = NSBundle.cz_currentVersion
        
        // 2. 获取`之前`的版本，保存在用户偏好 1.2
        let versionKey = "cn.itcast.versionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(versionKey)
        
        // 3. 将当前版本保存在用户偏好
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: versionKey)
        
        print("是否新版本 \(currentVersion > sandboxVersion)")
        // 4. 返回两个版本的比较
        return currentVersion > sandboxVersion
    }





}